package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TActiJoinProd;
import com.tencent.wxcloudrun.dto.ActiJoinProdReqDTO;
import com.tencent.wxcloudrun.mapper.TActiJoinProdMapper;
import com.tencent.wxcloudrun.service.TActiJoinProdService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_acti_join_prod(活动参选商品表)】的数据库操作Service实现
 * @createDate 2025-04-22 16:43:56
 */
@Service
public class TActiJoinProdServiceImpl extends ServiceImpl<TActiJoinProdMapper, TActiJoinProd>
    implements TActiJoinProdService {

    @Override
    public void joinToDb(Long userId, Long actiId, List<ActiJoinProdReqDTO> prodList) {
        if (CollectionUtils.isEmpty(prodList)) {
            return;
        }
        List<TActiJoinProd> actiJoinProdList = new ArrayList<>(prodList.size());
        for (ActiJoinProdReqDTO actiJoinProdReqDTO : prodList) {
            TActiJoinProd actiJoinProd = new TActiJoinProd();
            actiJoinProd.setActiId(actiId);
            actiJoinProd.setUserId(userId);
            actiJoinProd.setProdId(actiJoinProdReqDTO.getProdId());
            actiJoinProd.setQuantity(actiJoinProdReqDTO.getQuantity());
            actiJoinProd.setPrice(actiJoinProdReqDTO.getPrice());
            actiJoinProd.setAmount(actiJoinProdReqDTO.getAmount());
            actiJoinProd.setCreatedBy(String.valueOf(userId));
            actiJoinProd.setCreatedTime(LocalDateTime.now());
            actiJoinProd.setUpdatedBy(String.valueOf(userId));
            actiJoinProd.setUpdatedTime(LocalDateTime.now());
            actiJoinProdList.add(actiJoinProd);
        }
        this.saveBatch(actiJoinProdList);
    }

    @Override
    public void deleteJoin(Long actiId, Long userId) {
        LambdaQueryWrapper<TActiJoinProd> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TActiJoinProd::getActiId, actiId);
        queryWrapper.eq(TActiJoinProd::getUserId, userId);
        queryWrapper.eq(TActiJoinProd::getEnableFlag, 1);
        this.remove(queryWrapper);
    }
}
